/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 5/5/13
 * Time: 11:09 PM
 * To change this template use File | Settings | File Templates.
 */
package societyGames.codeAlong.model
{
import societyGames.xmlFragment.XMLFragment;
import societyGames.xmlFragment.XMLFragmentEditList;

public class Module extends XMLFragment
{
  private var _solution:Solution;

  public function get id():String
  {
    return _xml.@id;
  }

  [Bindable]
  public function set id(value:String):void
  {
    if (shipped)
    {
      throw new Error("Cannot modify id of shipped module.");
    }

    if (_solution == null)
    {
      _solution = ModulesDocument.instance.solutionsContainer.getSolution(_xml.@id);
    }
    _solution.id = value;
    _xml.@id = value;
  }

  public function get shipped():Boolean
  {
    if (!_xml.hasOwnProperty("@shipped"))
    {
      return true;
    }
    return _xml.@shipped != "false"; //@shipped will only be present and only be false if it hasn't shipped yet.
  }

  [Bindable]
  public function set shipped(value:Boolean):void
  {
    if (value)
    {
      delete _xml.@shipped;
    }
    else
    {
      _xml.@shipped = "false";
    }
  }

  public function get name():String
  {
    return _xml.@name;
  }

  [Bindable]
  public function set name(value:String):void
  {
    _xml.@name = value;
  }

  [Bindable]
  [FragmentCollection(className="societyGames.codeAlong.model.Case", nodeName="case")]
  public var cases:XMLFragmentEditList;

  [Bindable]
  [FragmentCollection(className="societyGames.codeAlong.model.Term", nodeName="term")]
  public var terms:XMLFragmentEditList;

  public function get goal():String
  {
    return _xml.goal[0].toString();
  }

  [Bindable]
  public function set goal(value:String):void
  {
    _xml.goal[0].setChildren(value);
  }

  public function get instruction():String
  {
    return _xml.hint[0].toString();
  }

  [Bindable]
  public function set instruction(value:String):void
  {
    _xml.hint[0].setChildren(value);
  }

  [Bindable]
  [Fragment(nodeName="codeBegin")]
  public var codeBegin:EntryReplaceable;

  [Bindable]
  [Fragment(nodeName="codeEnd")]
  public var codeEnd:EntryReplaceable;

  [Bindable]
  [Fragment(nodeName="library")]
  public var library:Library;

  override protected function getDefaultXML():XML
  {
    //Find the highest free module id.
    var freeId:int = 0;
    for each (var module:Module in ModulesDocument.instance.modulesContainer.modules)
    {
      var moduleId:int = int(module.id);
      if (moduleId >= freeId)
      {
        freeId = moduleId + 1;
      }
    }
    return <module
          id={freeId.toString()}
          shipped="false"
          name="New Module"
          >
            <case swf="RobotZero/RobotZeroContainer.swf" class="com.societyGames.robotExercises.RobotCodeExercise">
              <simulation>
                <robotX>1</robotX>
                <robotY>1</robotY>
              </simulation>
            </case>
            <goal>Guide the robot home.</goal>
            <hint>
              <![CDATA[<span class="summary"><a class="term" href="event:term:code">Code</a> is a list of instructions for the computer. When the computer <a class="term" href="event:term:run code">runs code</a> it performs the encoded instructions.</span><br><br><span class="directions">1) Copy the following code into the <a href="event:highlight:codeArea">Code Area</a>:<br><span class="code"><br>moveRight();<br>moveDown();</span><br><br>2) Click the <a href="event:highlight:runButton">Run Button</a>.<br><br>3) On success, click the <a href="event:highlight:nextButton">Next Button</a>.</span>]]>
            </hint>
            <codeBegin entry="codeBeginStatements"/>
            <codeEnd entry="codeEndStatements"/>
            <library>
              <function name="functionName" description="Function description.">
                <return type="bool" description="Return value description"/>
                <parameter name="parameter1" index="1" type="Boolean" description="True to arrest Alex, false otherwise."/>
                <example>functionName(true);</example>
              </function>
            </library>
            <syntaxErrors/>
            <exceptions/>
          </module>;
  }
}
}
